package test;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-07-24
 * Time:19:34
 */
public class ShellSort implements ISort{
    /**
     * gap是组数
     */
    public void sort(int[] array){
        System.out.println("希尔排序：");
        int[] gaps={5,3,1};
        for (int i = 0; i < gaps.length; i++) {
            shellSort(array,gaps[i]);
        }
    }

    public void shellSort(int[] array,int gap){
        for (int i = 0; i < array.length; i++) {
            int j = i-gap;
            int tmp=array[i];
            //不能忽略j=0的情况
            for (; j >= 0; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
}
